<template>
	<view class="shoplist">
		<view class="white_b customer-info r-5">
			<view class="align_center select-address" @click="modifyAddress">
				<view class="">
					<image :src="imgRemote+'assets/img/shoplistadd.png'"></image>
				</view>
				<view class="detail-address">
	
					<view class="" v-if="myAddress.province">
	
						<text v-if="myAddress.province">{{myAddress.province}}</text>
						<text v-if="myAddress.city">{{myAddress.city}}</text>
						<text v-if="myAddress.area">{{myAddress.area}}</text>
						<text v-if="myAddress.address">{{myAddress.address}}</text>
	
					</view>
					<view v-else>
						请先添加收货地址
					</view>
					<view class="fs-13" style="margin-top: 10rpx;">
						<text v-if="myAddress.shou_name">{{myAddress.shou_name}}</text>
						<text v-if="myAddress.shou_phone" style="margin-left:20rpx;">{{myAddress.shou_phone}}</text>
					</view>
				</view>
				<view>
					<text class="iconfont iconfanhui t-180 gray_font"></text>
				</view>
			</view>
		</view>
		
		<view class="shop white_b r-5 all-good">
			<view v-for="(item,index) in info.goods" :key="index">
				<view class="flex">
					<image class="have_img r-5" :src="item.mimage" mode="aspectFit"></image>
					<view class="include_delete  flex_left_right flex-full">
						<view class="info">
							<view style="width:100%;">
								<view class="bold two-line">{{item.name}}</view>
								<view style="margin-top: 20rpx;" class="flex_left_right">
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<view style="height:60px;">
		</view>
		
		<view class="fixed-bottom white_b width" >
			<view class="align_center price-info" :class="[system=='ios'?'pm':'']">
				<view class="pay-button" @click="pay">免费领取</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	const app = getApp().globalData;
	const {
		imgRemote,
	} = app;
	export default{
		data(){
		   return{
			   imgRemote: imgRemote,
			   params:{id:''},
			   info:{},
			   addressInfo: {},
			   hasAddress:false,
			   myAddress: {},
			   shop:[],
			   system:''
		   }	
		},
		onLoad(data) {
			let scene = decodeURIComponent(data.q);
			if (scene=='undefined') return;
			let code = scene.split('?sqrcode=');
			this.params.id = code[code.length - 1];
		},
		onShow() {
			this.system=uni.getSystemInfoSync().platform;
			if (uni.getStorageSync('userToken')) {
				this.getInfo();
			} else {
				getApp().globalData.isReload = true;
				uni.navigateTo({
					url: '../account/wxlogin?type=2'
				})
			}
			
			if (uni.getStorageSync('address')) {
				let useAddress = JSON.parse(uni.getStorageSync('address'));
				this.hasAddress = 1;
				console.log(useAddress)
				this.myAddress = useAddress;
			} else {
				this.defaultAddress();
			}
			
		},
		methods:{
			pay(){
				let _ = this;
				if (this.hasAddress == 0) {
					this.modifyAddress();
					return this.$Toast('请先添加收货地址');
				}
				let {
					tax_name,
					tax_num
				} = _.addressInfo;
				
				let {
					province,
					city,
					area,
					address,
					shou_name,
					shou_phone
				} = _.myAddress;
				
				if (!province) {
					return _.$Toast('请先填写收货地址');
				}
				if (!shou_name &&
					!shou_phone) {
					return _.$Toast('请先填写收货人和收货人电话');
				}
				let params=JSON.parse(JSON.stringify(_.params));
				params.address_id=_.myAddress.id;
				params.token=uni.getStorageSync('userToken');
				this.$post(this.$api.giftSubmit,params,(res)=>{
					
					if(res.data.code==1){
						 _.$Toast(res.data.msg);
						setTimeout(()=>{
							uni.switchTab({
								url:'/pages/tabar/user'
							})
						},500);
					}else{
						return _.$Toast(res.data.msg);
					}
				});
				
			},
		  	getInfo(){
			  this.$get(this.$api.gift, this.params, (res) => {
				  this.info=res.data.data;
				  });
			},
			defaultAddress() {
				let params = {
					token: uni.getStorageSync('userToken'),
					limit: 1000
				}
				this.$get(this.$api.aMy, params, (res) => {
					let {
						data
					} = res;
					if (data.code == 1) {
						let info = data.data.data.filter((item) => {
							return item.is_check == 2;
						})
						this.hasAddress = info.length;
						this.myAddress = info[0];
						if (this.hasAddress == 0) {
							this.myAddress = {}
						}
					}
				})
			},
			modifyAddress() {
				uni.navigateTo({
					url: '/pages/addressMange/index'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	input {
		margin-top: 3rpx;
	}
	
	.blank input {
		display: block;
	}
	
	.customer-info {
		image {
			width: 50rpx;
			height: 58rpx;
		}
	
		.select-address {
			border-bottom: 2px dashed #eee;
			padding-bottom: 30rpx;
		}
	
		margin:30rpx;
		padding: 30rpx;
	}
	
	.detail-address {
		width: 550rpx;
		padding: 0 0rpx 0 20rpx;
	}
	
	.all-good {
		padding: 0 30rpx;
		margin: 0 30rpx;
	}
	
	.single_good {
		padding-top: 30rpx;
	
		.good_img {
			width: 180rpx;
			height: 120rpx;
			margin-right: 30rpx;
	
	
		}
	}
	
	.have_img {
		width: 200rpx;
		height: 200rpx;
		margin: 30rpx 30rpx 0 0;
	}
	
	.total-price {
		border-top: 1px solid #eee;
		margin-top: 30rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: right;
	}
	
	.order-remark {
		height: 83rpx;
	
	
	}
	
	.pay-method {
		border-radius: 10rpx;
		padding-bottom: 20rpx;
	
		.radio-pay {
			padding: 0 16rpx 0 20rpx;
		}
	
		margin:0 30rpx;
	
		.way {
			height: 82rpx;
			line-height: 82rpx;
		}
	
		.iconfont {
			margin-right: 19rpx;
			font-size: 40rpx;
		}
	
		.border-color {
			border-top: 1px solid #EEE;
			margin: 20rpx 12rpx 20rpx 8rpx;
		}
	
	
	}
	
	.fixed-bottom {
		position: fixed;
		bottom: 0;
		z-index: 99;
	
		.price-info {
			justify-content: flex-end;
			box-sizing: border-box;
			padding: 15rpx 0;
			.pay-button {
				background: #FF7133;
				color: white;
				margin-left: 30rpx;
				margin-right: 30rpx;
				
				width: 100%;
			}
		}
	}
	.pm{
		margin-bottom: 45rpx;
	}
</style>